layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func','laydate'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    layui.use(['ax'], function () {
        var $ax = layui.ax;
        var ajax = new $ax(Feng.ctxPath + "/kmsPage/getChartsData", function (data) {
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('main'));
            var value = 0.2;
            myChart.setOption({
                series: [{
                    type: 'liquidFill',
                    data: data
                }]
            });
        }, function (data) {
            Feng.success("归档路径:"+data.responseText);
        });
        ajax.start();
    });
    layui.use(['ax'], function () {
        var $ax = layui.ax;
        var ajax = new $ax(Feng.ctxPath + "/kmsPage/getChartsData", function (data) {
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(document.getElementById('main'));
            var value = 0.2;
            myChart.setOption({
                series: [{
                    type: 'liquidFill',
                    data: data
                }]
            });
        }, function (data) {
            Feng.success("归档路径:"+data.responseText);
        });
        ajax.start();
    });


    /**
     * 系统管理--角色管理
     */
    var sysQnlKeyTable = {
        tableId: "sysQnlKeyTable",    //表格id
        condition: {
            KmsName: ""
        }
    };

    /**
     * 初始化表格的列
     */
    sysQnlKeyTable.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: '密钥id'},
            {field: 'key', align: "center", sort: true, title: '密钥'},
            {field: 'status', align: "center", sort: true, title: '状态'},
            {field: 'poolSize', align: "center", sort: true, title: '密钥池大小'},
            {field: 'destSite', align: "center", sort: true, title: '目标站点'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    sysQnlKeyTable.search = function () {
        var queryData = {};
        queryData['status'] = $("#status").val();
        console.log(queryData);
        table.reload(sysQnlKeyTable.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加角色
     */
    sysQnlKeyTable.openAddKms = function () {
        func.open({
            height: 470,
            title: '生成密钥',
            content: Feng.ctxPath + '/kmsPage/kms_add',
            tableId: sysQnlKeyTable.tableId
        });
    };

    /**
     * 点击编辑角色
     *
     * @param data 点击按钮时候的行数据
     */
    sysQnlKeyTable.onEditsysQnlKeyTable = function (data) {
        func.open({
            height: 470,
            title: '修改角色',
            content: Feng.ctxPath + "/Kms/Kms_edit?KmsId=" + data.KmsId,
            tableId: sysQnlKeyTable.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    sysQnlKeyTable.exportExcel = function () {
        var checkRows = table.checkStatus(sysQnlKeyTable.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除角色
     *
     * @param data 点击按钮时候的行数据
     */
    sysQnlKeyTable.onDeleteKms = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/delete", function () {
                Feng.success("删除成功!");
                table.reload(Kms.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.kmsId);
            ajax.start();
        };
        Feng.confirm("是否删除秘钥 " + data.key + "?", operation);
    };

    /**
     * 分配菜单
     *
     * @param data 点击按钮时候的行数据
     */
    sysQnlKeyTable.KmsAssign = function (data) {
        parent.layer.open({
            type: 2,
            title: '权限配置',
            area: ['300px', '450px'], //宽高
            fix: false,
            maxmin: true,
            content: Feng.ctxPath + '/Kms/Kms_assign/' + data.KmsId,
            end: function () {
                table.reload(sysQnlKeyTable.tableId);
            }
        });
    };

    //渲染时间选择框
    laydate.render({
        elem: '#beginTime'
    });

    //渲染时间选择框
    laydate.render({
        elem: '#endTime'
    });
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + sysQnlKeyTable.tableId,
        url: Feng.ctxPath + '/sysQnlKey/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: sysQnlKeyTable.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        sysQnlKeyTable.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        let $beginTime = $("#beginTime").val();
        let $endTime = $("#endTime").val();
        if($beginTime === null || $beginTime === 'null' || $beginTime === '') {
            Feng.error("开始时间不能为空");
            return;
        }
        if($endTime === null || $endTime === 'null' || $endTime === '') {
            Feng.error("开始时间不能为空");
            return;
        }
        var ajax = new $ax(Feng.ctxPath + "/kmsPage/downKey?beginTime="+$beginTime+"&endTime="+$endTime, function (data) {
            Feng.success("归档路径",data);
        }, function (data) {
            Feng.success("归档路径:"+data.responseText);
        });
        ajax.start();
    });

    // 工具条点击事件
    table.on('tool(' + sysQnlKeyTable.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            sysQnlKeyTable.onEditKms(data);
        } else if (layEvent === 'delete') {
            sysQnlKeyTable.onDeleteKms(data);
        } else if (layEvent === 'KmsAssign') {
            sysQnlKeyTable.KmsAssign(data);
        }
    });
});
